package com.sunmnet.j2ee.action.powerdb.designer;

import com.sunmnet.j2ee.action.BaseAction;
import com.sunmnet.j2ee.action.BaseNewAction;
import com.sunmnet.j2ee.bean.pub.action.JsonResult;
import com.sunmnet.j2ee.bean.pub.action.PageResult;
import com.sunmnet.j2ee.core.pub.QueryFilter;
import com.sunmnet.j2ee.core.pub.annotation.MethodName;
import com.sunmnet.j2ee.entity.powerdb.designer.Version;
import com.sunmnet.j2ee.service.BaseService;
import com.sunmnet.j2ee.service.powerdb.designer.VersionService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;


@Controller("/powerdb/designer/version")
@RequestMapping("/powerdb/designer/version")
public class VersionAction extends BaseNewAction<Version,Long> {

    @Override
    @Resource(name="/powerdb/designer/versionService")
	public void setService(BaseService<Version, Long> service) {
       super.service=service;
	}

	/**
	 * 批量查询
	 *
	 * @param request
	 * @return JsonResult
	 */

	@MethodName(name = "批量查询")
	@RequestMapping(value = "/", method = RequestMethod.PATCH)
	@ResponseBody
	public JsonResult find(HttpServletRequest request){
		QueryFilter filter = new QueryFilter(request);
		return super.find(filter);
	}
	/**
	 * 批量查询
	 *
	 * @param request
	 * @return JsonResult
	 */

	@MethodName(name = "批量查询")
	@RequestMapping(value = "/pages", method = RequestMethod.PATCH)
	@ResponseBody
	public PageResult findPages (HttpServletRequest request){
		QueryFilter filter = new QueryFilter(request);
		return super.findPages(filter);
	}
	/**
	 * 查询
	 *
	 * @param id
	 * @return JsonResult
	 */

	@MethodName(name = "对象查询")
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	public JsonResult get (@PathVariable("id") long id, HttpServletRequest request){
		return super.get(id);
	}

	/**
	 * 新增
	 *
	 * @param obj
	 * @return JsonResult
	 */

	@MethodName(name = "新增")
	@RequestMapping(value = "/", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult add (Version obj){

		JsonResult jr = new JsonResult();
		try {
			Date e = new Date();
			obj.setCreateTime(e);
			obj.setModifyTime(e);
			service.save(obj);
			jr.setMsg("保存成功！");
			((VersionService) service).saveVersionOfProject(obj);
			jr.setObj(obj);
		}catch (Exception e){
			service.delete(obj.getId());
			setErrMsg("新增版本出错",LOG,jr,e);
		}
		return jr;
	}
	/**
	 * 更新
	 *
	 * @param obj
	 * @return JsonResult
	 */

	@MethodName(name = "修改")
	@RequestMapping(value = "/", method = RequestMethod.PUT)
	@ResponseBody
	public JsonResult update (Version obj){
		return super.update(obj);
	}
	/**
	 * 删除
	 *
	 * @param id
	 * @return JsonResult
	 */

	@MethodName(name = "删除")
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	public JsonResult delete ( @PathVariable("id") long id){
		return super.delete(id);
	}


	/**
	 * 批量删除
	 *
	 * @param ids 一个或多个id "1,2,3"
	 * @return JsonResult
	 */

	@MethodName(name = "批量删除")
	@RequestMapping(value = "/batch", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult batchDelete (@RequestParam(value = "ids", required = true) String ids){
		return super.delete(ids);
	}

	/**
	 * 根据工程id,从版本表获取到关联的表
	 */
	@MethodName(name = "根据工程id获取相关版本")
	@RequestMapping(value = "/getProjectRelVersion/{id}", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult getProjectRelVersion(@PathVariable("id") long projectId) {

		JsonResult j = new JsonResult(true);

		try {
			j.setObj(((VersionService) service).getProjectRelVersion(projectId));
			j.setMsg("查询成功！");
		} catch (Exception e) {
			BaseAction.setErrMsg("查询失败", LOG, j, e);
		}
		return j;
	}
}